package pack1021.exception;

import java.util.InputMismatchException;
import java.util.Scanner;

// 多异常情况
public class DemoB {
    private static Scanner scan;

    static {
        scan = new Scanner(System.in);
    }

    private static void a() {
        int[] array = {4, 5, 6, 7, 0};
        System.out.print("请输入数组的下标:");
        try {
            int index = scan.nextInt(); // 输入的类型不匹配
            int array_number = array[index]; // 越界
            int result = 9 / array_number; // 数学运算
            System.out.println("计算后的结果是：" + result);
        } catch (InputMismatchException e) { // 匹配输入不合法的异常
            System.out.println("你输入的不合法，只能输入数字！");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("输入的长度越界");
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0！");
        }
        System.out.println("运算结束");
    }

    // 多异常catch：先写子类，后写父类
    private static void b() {
        int[] array = {4, 5, 6, 7, 0};
        System.out.print("请输入数组的下标:");
        try {
            int index = scan.nextInt(); // 输入的类型不匹配
            int array_number = array[index]; // 越界
            int result = 9 / array_number; // 数学运算
            System.out.println("计算后的结果是：" + result);
        } catch (InputMismatchException e) { // 匹配输入不合法的异常
            System.out.println("你输入的不合法，只能输入数字！");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("输入的长度越界");
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0！");
        } catch (Exception e) {
            System.out.println("出现问题了！");
        }
        System.out.println("运算结束");
    }

    public static void main(String[] args) {
        b();
    }
}